﻿using Catastrophe.BasePC.UI.SystemIni.BaseData;
using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Catastrophe.BasePC.UI.UIMode
{
    /// <summary>
    /// 部门
    /// </summary>
    public partial class UcSysDepartmentModel : ObservableObject
    {
        /// <summary>
        /// 
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<SysDictionaryEntity> sysDictionaryEntities;
        /// <summary>
        /// 选中的
        /// </summary>
        [ObservableProperty]
        private SysDictionaryEntity sysDictionaryEnity;
        /// <summary>
        /// 名称
        /// </summary>
        [ObservableProperty]
        private string name;
        /// <summary>
        /// 值
        /// </summary>
        [ObservableProperty]
        private string value;

        SysDictionaryDal sysDictionaryDal { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string key {  get; set; }

        /// <summary>
        /// 查询
        /// </summary>
        public ICommand SeleceData { get; set; }

        /// <summary>
        /// 添加数据
        /// </summary>
        public ICommand AddData { get; set; }

        /// <summary>
        /// 删除
        /// </summary>
        public ICommand DelData { get; set; }
        public UcSysDepartmentModel()
        {
            sysDictionaryDal = new SysDictionaryDal();
            sysDictionaryEntities = new ObservableCollection<SysDictionaryEntity>();
            SeleceData = new RelayCommand(SeleceDataMethod);
            AddData = new RelayCommand(AddDataMethod);
            DelData = new RelayCommand(DelDataMethod);
            //LoadData();
        }

        private void DelDataMethod()
        {
            sysDictionaryDal.DeleteByName(Name, key);
            LoadData();
        }

        private void AddDataMethod()
        {
            var dst = sysDictionaryDal.AddEntitySingle(new SysDictionaryEntity()
            {
                DicName = Name,
                DicKey = key,
                DicValue = Value,
                TimeStamp = DateTime.Now.ToString(),
                Id=DateTime.Now.ToFileTime(),
            });
            if (dst.Success)
            {
                LoadData();
            }
        }

        private void SeleceDataMethod()
        {
            LoadData();
        }

        private void LoadData()
        {
            sysDictionaryEntities.Clear();
            var data = sysDictionaryDal.QueryByKey(key);
            data.ForEach(x =>
            {
                sysDictionaryEntities.Add(x);
            });
        }
    }
}
